<?php

namespace App\Http\Controllers\Admin;


use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB,Session,
    Validator , 
    Hash;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
      $users= DB::table("user")
              ->where("user_name","like","%".$request->get("keyword")."%")
              ->orWhere("user_nickname","like","%".$request->get("keyword")."%")
              ->paginate(3);
      //模板显示
       $keyword = $request->get("keyword");
      return view("admin.user.index",["users" => $users,"keyword" =>$keyword]);
      
       //return view('admin.user.index');
    }
 
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
      return  view("admin.user.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            "user_name" => "required|unique:user",
            "user_pass" => "required|between:6,15",
            "repassword" => "required|same:user_pass",
            "user_nickname" => "required",
                ], [
            "user_name.required" => "账号不能为空",
            "user_name.unique" => "账号已被占用",
            "user_pass.required" => "密码不能为空",
            "user_pass.between" => "密码长度应为6-15位",
            "repassword.required" => "确认密码必须填写",
            "repassword.same" => "两次密码输入不一致",
            "user_nickname.required" => "昵称不能为空"
        ]);
        //提取出数据并重组
        $data = $request->except("_token", "repassword");
        $data["user_pass"] = Hash::make($data["user_pass"]);
        $data["user_regtime"] = date("Y-m-d H:i:s");
//        dd($data);
        //执行数据创建
        if (FALSE !== $insertID = DB::table("user")->insertGetId($data)) {
            return redirect("/admin/user");
        } else {
            return back()->with(["info" => "添加用户失败"]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        
         //查询该用户的记录
        $userRec = DB::table("user")->where("id", $id)->first();
//            dd($userRec);
//       
       return view("admin.user.edit", compact("userRec"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //验证数据的有效性
        $this->validate($request, [
            "user_pass" => "between:6,15",
            "repassword" => "same:user_pass",
            "user_nickname" => "required",
                ], [
            "user_pass.between" => "密码长度应为6-15位",
            "repassword.same" => "两次密码输入不一致",
            "user_nickname.required" => "昵称不能为空"
        ]);
        //修改用户的数据
        $data = $request->except("_token", "_method", "repassword");
        $data["user_regtime"] = date("Y-m-d H:i:s");
        //对密码重新构造
        if (!empty($data["user_pass"])) {
            $data["user_pass"] = Hash::make($data["user_pass"]);
        } else {
            unset($data["user_pass"]);
        }
        if (false != $affectedRows = DB::table("user")->where("id", $id)->update($data)) {
            return redirect("/admin/user");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
         if (false !== DB::table("user")->where("id", $id)->delete()) {
            return redirect("/tips")->with(["info" => "操作成功", "url" => "/admin/user"]);
        } else {
            return redirect("/tips")->with(["info" => "操作失败", "url" => "/admin/user"]);
        }
    }
    
}